package com.example.myapplication.apter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.SearchhousingBean;
import com.example.myapplication.house.HouseDetailsActivity;

import java.util.List;

public class SearchhousingAdapter extends RecyclerView.Adapter<SearchhousingAdapter.Myhot> {
    private Activity context;
    private View view;
    private List<SearchhousingBean.RowsBean> list;

    public SearchhousingAdapter(Activity context, List<SearchhousingBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public Myhot onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.item_house,null);
        return new SearchhousingAdapter.Myhot(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Myhot holder, int position) {
        SearchhousingBean.RowsBean rb = list.get(position);
        if(rb ==null){
            return;
        }
        Glide.with(context).load(TokenBean.IP+rb.getPic()).into(holder.ap_houseimg);
        holder.ap_houseimg.setScaleType(ImageView.ScaleType.FIT_XY);
        Log.e("SearchhousingAdapter",TokenBean.token+rb.getPic());
        holder.ap_housetitle.setText(rb.getSourceName());
        holder.ap_housetprice.setText(rb.getPrice());
        holder.ap_housetcontent.setText(rb.getDescription());
        String desc = rb.getDescription();
        if(desc!=null && desc.length()>20){
            holder.ap_housetcontent.setText(list.get(position).getDescription().substring(0,20)+"...");
        }else {
            holder.ap_housetcontent.setText(list.get(position).getDescription());
        }


        /*if(!TextUtils.isEmpty(list.get(position).getDescription())&&
                list.get(position).getDescription().length()>=20){
            holder.ap_housetcontent.setText(list.get(position).getDescription().substring(0,20)+"...");
        }else {
            holder.ap_housetcontent.setText(list.get(position).getDescription());
        }*/

        //房源详情
        holder.re_houserapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HouseDetailsActivity.class);
                intent.putExtra("SearchhousingBeanID",String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
                //context.startActivityForResult(intent,999);

            }
        });
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    class Myhot extends RecyclerView.ViewHolder{


        private final ImageView ap_houseimg;
        private final TextView ap_housetitle;
        private final TextView ap_housetprice;
        private final TextView ap_housetcontent;
        private final RelativeLayout re_houserapter;

        public Myhot(@NonNull View itemView) {
            super(itemView);
            ap_houseimg = itemView.findViewById(R.id.ap_houseimg);
            ap_housetitle = itemView.findViewById(R.id.ap_housetitle);
            ap_housetprice = itemView.findViewById(R.id.ap_housetprice);
            ap_housetcontent = itemView.findViewById(R.id.ap_housetcontent);
            re_houserapter = itemView.findViewById(R.id.re_houserapter);
        }
    }
}
